<?php
namespace Amaze\Controller;
use Think\Controller;
use Home\Service\ToolService;
use Think\Model;
class TeacherController extends Controller{


	/**
	 * 魔术方法，设置默认处理机制
	 * @param String $method
	 * @param String $args
	 */
	public function __call($method, $args){
		if (method_exists('Teacher', $method)){
			$this->$method;
		}else{
			$response['STATUS'] = "error";
			$response['ERRMSG'] = "no method";
			$response['RESULT'] = array();
			$this->ajaxReturn($response);
		}
	}



	/**
	 * 加载老师资料
	 * */
	public function listTeacher(){
		$M = new Model();
		$query = "select * from teacher";
		$teacher= $M->query($query);
		$this->assign("teacher",$teacher);
		$this->theme('admin')->display('admin-table');
	}
	//新增老师
	public function addTeacher(){
		
		$Form = M('teacher');
		$teacher = I('param.');
		$teacher['createtime']=date("Y-m-d h:i:s");
		$teacher['good']=0;
		$teacher['bad']=0;
		//$department=M('department')->where(array("id"=>$teacher['department']))->find();
		//$teacher['academy']=$department['name'];
		$school=M('activity')->where(array("teacher"=>$teacher['id']))->find();
		$teacher['id']=$school['teacher'];
		//图片
		$info = upload();
		if($info){
			$pic = "Uploads/teacher/".$info['pic']['savepath'].$info['pic']['savename'];
		}
		if(empty($pic)){
			$pic = "";
		}
		$teacher['pic']=$pic;
		$res=$Form->add($teacher);
		if(!empty($res)){
			$this->success('新增成功', 'listTeacher');
		}
		$this->error('新增失败');
	}
	//跳转至添加老师页面
	public function toAdd(){
		$this->theme('admin')->display('admin-teacher');
	}
	//修改头像
	public function changeTeachePic(){
		
		$tid = I('param.tid');
		
		if(empty($tid)){
			$this->error('修改失败 tid错误');
		}
		$info = upload();
		
		if($info){
			$pic = "Uploads/teacher/".$info['pic']['savepath'].$info['pic']['savename'];
		}
		if(empty($pic)){
			$this->error('修改失败 上传失败');
		}

		$teacher_table = M('teacher');
		$teacher['id']=$tid;
		$teacher['pic']=$pic;
		$res = $teacher_table->save($teacher);

		if(empty($res)){
			$this->error('修改失败 保存失败');
		}
		$response['RESULT']=array();
		$response['ERRMSG']="";
		$response['STATUS']="success";
		$this->success('修改成功');
		//图片
	}
	//保存修改的老师
	public function updateTeacher(){
		$teacher = I('param.');
		$res=M('teacher')->where(array("id"=>$teacher['id']))->save($teacher);
		if(!empty($res)){
			$this->success('修改成功', 'listTeacher');
		}
		$this->error('修改失败');
	}
	//编辑老师
	public function editTeacher(){
		$sid = I("param.tid");
		$teacher = M('teacher')->where(array("id"=>$sid))->find();
		if(empty($teacher)){
			$this->error("无法编辑");
		}
		$this->assign("teacher",$teacher);
		$this->theme('admin')->display('admin-editTeacher');
	}
	
}

?>
